import utils.FileUtils;
import utils.SystemLookup;

import java.math.BigInteger;

/**
 * @author dab
 */
public class LinearSearch {
    private static long elapsedNanoTime = 0;
    private static BigInteger elapsedTime = BigInteger.ZERO;

    public static void main(String[] args) {
        if(args.length != 2) {
            System.out.println("Niepoprawna ilość elementów");
            System.exit(1);
        }
        int searchNumber = 0;
        try {
            searchNumber = Integer.parseInt(args[1]);
        } catch (Exception ex) {
            System.out.println("Niepoprawne dane. Blad parsowania: int");
            System.exit(1);
        }
        String fileName = args[0];

        String[] out = FileUtils.readTextFileLine(SystemLookup.PATH_TO_LOAD + fileName + FileUtils.FILE_EXTENSION).split(" ");
        int[] arrayToSearch = new int[out.length];
        for (int i = 0; i < out.length; i++) {
            arrayToSearch[i] = Integer.parseInt(out[i]);
        }
        System.out.println("Array.lenght: " + arrayToSearch.length);
        //for (int i = 0; i < numberOfExec; i++) {
        //int[] intArray = Arrays.copyOf(arrayToSort, arrayToSort.length);
        long timeStart = System.nanoTime();
        linearSearch(arrayToSearch, searchNumber);
        elapsedNanoTime = System.nanoTime() - timeStart;
        elapsedTime = elapsedTime.add(BigInteger.valueOf(elapsedNanoTime));
        //}
        if(arrayToSearch.length>searchNumber)
            System.out.println(arrayToSearch[searchNumber]);
        System.out.println("\nNano Time: " + elapsedTime.toString());
        System.out.println("Milliseconds Time: " + elapsedTime.divide(new BigInteger("1000000")));

    }

    private static void linearSearch(int[] a, int x) {
        for (int i = 0; i < a.length; i++) {
            if(a[i] == x) {
                System.out.println("Odnaleziono element " + x + " pod indeksem: " + i);
                return;
            }
        }
        System.out.println("Nie znaleziono w tablicy elementu: " + x);
    }
}
